|
|
|
|
|
|
|
Adding Code to the Document |
|
|
|
|
|
|
|
|
After you create the interface of the document by using Visual Basic controls, you are ready to write the code that makes the document perform a task. As with the forms in a standard program, you write code for events of the controls in the document. All code work is done in the Code window. You can access the Code window by double-clicking a control or by clicking the View Code button in the Project window. For the sample application, you need to enter the code from Listing 32.1 in the Click event of the command button. |
|
|
|
|
|
|
|
|
Listing 32.1 CALCDOC.DOBPlacing Code in the Click Event to Run the Calculation |
|
|
|
|
|
|
|
|
Private Sub cmdCalculate_Click()
Dim m_Principal As Single, m_Interest As Single
Dim m_Payment As Single, m_Term As Integer
Dim m_fctr As Single
m_Principal = Val(txtPrincipal.Text)
m_Interest = Val(txtInterest.Text) / 1200
m_Term = Val(txtTerm.Text)
m_fctr = (1 + m_Interest) ^ (m_Term * 12)
m_Payment = m_Interest * m_fctr * m_Principal / (m_fctr - 1)
txtPayment.Text = Format(m_Payment, "Fixed")
End Sub |
|
|
|
|
|
|
|
|
Although the code in Listing 32.1 exists in a UserDocument object, it works just like code inside a form. First the values entered by the user are retrieved from the text boxes and stored in local variables. Next the code calculates the payment and displays the results in the text box txtPayment. |
|
|
|
|
|
|
|
|
Testing Your ActiveX Document |
|
|
|
|
|
|
|
|
After entering the code and saving your document, you are ready to test the code. Testing an ActiveX document is a little different than testing a standard program because the document must run inside another application. To determine what type of application runs when you start the project, display the Debugging tab of the Properties dialog box. This dialog box has several options, as you can see in Figure 32.5. |
|
|
|
|
|
|
|
|
The default option in the Debugging tab, Start Component, allows the code in the component to determine the manner in which it starts. If you have Internet Explorer installed, and you start an ActiveX Document project, this default setting causes the application to be loaded automatically into Internet Explorer. Try it now with the sample project. Simply press F5 or click the VB Start button, and you should see the Web version of the Loan Calculator appear, as shown in Figure 32.6. |
|
|
|
|
|